<?php
namespace app\gym\controller;
use app\common\controller\AdminBase;
/*pt 课时*/
class PtCourse extends AdminBase{
	private $shop_id;
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();
		$this->shop_id=sp_get_current_shop_id();
    }
	/*列表*/
	function index(){
		echo $this->fetch();
	}	
	//删除
	function delete(){
		$id=input("id");
		db("Demo")->where("id =$id and shop_id=".$this->shop_id)->delete();
		$this->ajax_success("成功");
	}
	//编辑
	function edit(){
		$model=db("Demo")->where("shop_id=".$this->shop_id." and id=".input("id"))->find();
		$this->assign("model",$model);
		echo $this->fetch();	
	}
	function edit_post(){
		if(request()->isPost()){//提交
			db("Demo")->where("shop_id=".$this->shop_id." and id=".input("id"))->update(array("name"=>input("name")));
			$this->ajax_success("成功");
		}
	}
	//新增
	function add(){
		if(request()->isPost()){
			db("Demo")->insert(array("name"=>input("name"),"shop_id"=>$this->shop_id));
		    $this->ajax_success("成功");
		}else{
			echo $this->fetch();	
		}
	}
	//tab
	function step(){
		echo $this->fetch();
	}
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	function  ajax_data(){
		$q=input("q","x");
		$list=db("Demo")->where("name like '%$q%'")->field("id,name as text")->select();
		header("Content-type:application/json;charset=utf-8");
		exit(json_encode($list,TRUE));
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10071001");
		$this->assign("menus",$auth_group);
	}
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'name'=>array("field" =>"name","operator" =>"="),
		 );
		$shop_id=$this->shop_id;;
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		$where_and.=" and shop_id=$shop_id";
		$total=db("Demo")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("Demo")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
}
